package serverMain;
// <editor-fold defaultstate="collapsed" desc="Imports">
import server_V1.ProtocallThreadV1;
import server_V2.ProtocallThreadV2;
import java.io.IOException;
import java.net.ServerSocket;
import java.util.logging.Level;
import java.util.logging.Logger;
// </editor-fold> 

/**
 * @file ServerThread.java
 *
 * @version 0.1
 * @author Chris Sewell 10008342
 */
public class ServerThread extends Thread {
    // <editor-fold defaultstate="collapsed" desc="Attributes">

    int version;
    ServerSocket acceptSocket;
    ServerCounter downloadCount;
    // </editor-fold> 
    // <editor-fold defaultstate="collapsed" desc="Constructors">

    public ServerThread(int port, int version) throws IOException {

        this.version = version;
        acceptSocket = new ServerSocket(port);
        downloadCount = new ServerCounter();//Initialise download counter

    }
    // </editor-fold> 
    // <editor-fold defaultstate="collapsed" desc="Methods">

    @Override
    public void run() {
        switch (version) {
            case 1: {//User has requested version 1
                while (true) {
                    ProtocallThreadV1 newClient;//Create new client object
                    try {
                        /**New connection create thread and socket for client
                         * Start the thread
                         */
                        newClient = new ProtocallThreadV1(acceptSocket.accept(), downloadCount);
                        newClient.start();
                    } catch (IOException ex) {
                        Logger.getLogger(ServerThread.class.getName()).log(Level.SEVERE, null, ex);
                        break;
                    }

                }
            }
            case 2: {//User has requested version 2
                while (true) {
                    ProtocallThreadV2 newClient;//Create new client object
                    try {
                        /**New connection create thread and socket for client
                         * Start the thread
                         */
                        newClient = new ProtocallThreadV2(acceptSocket.accept(), downloadCount);
                        newClient.start();
                    } catch (IOException ex) {
                        Logger.getLogger(ServerThread.class.getName()).log(Level.SEVERE, null, ex);
                        break;
                    }

                }
            }

        }

    }
    // </editor-fold> 
}